package com.wx.session;

import java.util.ArrayList;
import java.util.List;

public class Session {
	private String openid;
	private static long time;
	private List<SessionData> sessionData = new ArrayList<SessionData>();

	public static long getTime() {
		return time;
	}

	public static void setTime(long time) {
		Session.time = time;
	}

	public List<SessionData> getSessionData() {
		return sessionData;
	}

	public void setSessionData(ArrayList<SessionData> sessionData) {
		this.sessionData = sessionData;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	// 计时器减一分钟
	public void timeMin() {
		time = time - 60 * 1000;
	}

	// 重置计时器，10分钟
	public void setTime() {
		time = 10 * 60 * 1000;
	}

	// 判断计时器是否为零
	public boolean timeStop() {
		if (time == 0)
			return true;
		else
			return false;
	}

	public long whatTime() {
		return time;
	}

	// 添加或修改
	public void set(SessionData data) {
		int i = search(data.getName());
		setTime();
		if (i < 0) {// 添加
			sessionData.add(data);
		} else {
			sessionData.set(i, data);
		}
	}

	// 移除
	public void remove(String name) {
		int i = search(name);
		if (i >= 0) {
			sessionData.remove(i);
		}
	}

	public int search(String name) {
		if (!sessionData.isEmpty()) {
			setTime();
			for (int i = 0; i < sessionData.size(); i++) {
				if (sessionData.get(i).getName().equals(name))
					return i;
			}
		}
		return -1;
	}

	public String get(String name) {
		int i = search(name);
		setTime();
		if (i >= 0) {
			return sessionData.get(i).getValue();
		}
		return null;
	}
}
